<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;

class Login extends Controller
{
	// 公共
	public function _initialize()
	{
		if(Session::has('admin','mmjtAdmin')){
			$this->redirect("index/index");
		}
	}


    public function index()
    {
        if(request()->isAjax()){
            $name = htmlspecialchars_decode(strip_tags(input('post.name')));
            $password = md5(htmlspecialchars_decode(strip_tags(input('post.password'))));
            $captcha = strip_tags(htmlspecialchars_decode(input('post.yzm')));

        	if($name && $password){
        		if(!$captcha){
        			return ['err' => '请输入验证码'];
        		}
        		if(!captcha_check($captcha)){
					return ['err' => '验证码错误'];
				};

        		$admin = db('admin')->where(['name' => $name,'password' => $password])->find();

				if($admin){
					if($admin['hide'] <= 0){
						$data = ["err" => '账户被禁用'];
					}else{
						db('admin')->where('id',$admin['id'])->update(['update_time' => time(),'update_ip' => _ip()]);
						Session::set('admin',$admin,'mmjtAdmin');
						$data = ["ret" => '登录成功,正在跳转...'];
					} 
				}else{
					$data = ["err" => '账户和密码错误'];
				}
        	}else{
			 	$data = ['err' => '请输入信息'];
        	}
        	return json($data);
		}else{
            $icp = db('config_system')->where('title', 'bottom')->cache(86400)->value('content');

            $this->assign('icp', $icp);

			return $this->fetch();
		}
    }
}
